package hhm.android.base.activity.mainActivity

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import hhm.android.base.R
import hhm.android.base.activity.BaseActivity


open class MainActivity : BaseActivity() {

    private val fragmentList = ArrayList<Fragment>()
    private val buttonViewList = ArrayList<MyViewHolder>()
    private val buttonRescouseList = ArrayList<ButtonResourceBean>()
    lateinit var transaction: FragmentTransaction
    lateinit var ll: LinearLayout
    private var listener: MainBottomButtonClickListener? = null
    var currentSelectBottom = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.frame_base_activity_main)
        ll = findViewById(R.id.frame_base_activity_main_ll)
    }

    protected fun setFragmentAndButton(
        fList: ArrayList<Fragment>,
        bList: ArrayList<ButtonResourceBean>,
        mainColor: Int
    ) {
        setFragmentAndButton(fList, bList, mainColor, null)
    }

    protected fun setFragmentAndButton(
        fList: ArrayList<Fragment>,
        bList: ArrayList<ButtonResourceBean>,
        mainColor: Int,
        listener: MainBottomButtonClickListener?
    ) {
        this.fragmentList.addAll(fList)
        this.buttonRescouseList.addAll(bList)
        if (fList.size == 0 || bList.size == 0 || fList.size != bList.size) {
            throw Exception("两个列表长度需一致且不能为空")
        }
        this.listener = listener
        init(mainColor)
    }

    protected fun reSetData(
        fList: ArrayList<Fragment>,
        bList: ArrayList<ButtonResourceBean>,
        mainColor: Int,
        listener: MainBottomButtonClickListener?
    ) {
        transaction = supportFragmentManager.beginTransaction()
        this.fragmentList.forEach {
            transaction.remove(it)
        }
        transaction.commitAllowingStateLoss()
        this.fragmentList.clear()
        this.buttonRescouseList.clear()
        this.buttonViewList.clear()
        ll.removeAllViews()

        setFragmentAndButton(fList, bList, mainColor, listener)
    }

    private fun init(mainColor: Int) {
        val outMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(outMetrics)
        val widthPixels = outMetrics.widthPixels
        buttonRescouseList.forEachIndexed { index, it ->
            val item =
                LayoutInflater.from(this).inflate(R.layout.activity_main_bottom_item, null, false)
            val iv = item.findViewById<ImageView>(R.id.activity_main_bottom_iv)
            val tv = item.findViewById<TextView>(R.id.activity_main_bottom_tv)
            tv.text = it.text
            if (index == 0) {
                transaction = supportFragmentManager.beginTransaction()
                transaction.add(R.id.frame_base_activity_main_frame, fragmentList[index])
                tv.setTextColor(ContextCompat.getColor(this, mainColor))
                iv.setImageResource(it.selectResource)
            } else {
                transaction.add(R.id.frame_base_activity_main_frame, fragmentList[index])
                transaction.hide(fragmentList[index])
                tv.setTextColor(ContextCompat.getColor(this, R.color.text_grey))
                iv.setImageResource(it.firstRescouse)
            }
            if (index == buttonRescouseList.size - 1) {
                transaction.commit()
            }
            val vh = MyViewHolder(item, iv, tv)
            buttonViewList.add(vh)
            ll.addView(item)
            val p = item.layoutParams
            p.width = widthPixels / buttonRescouseList.size
            item.layoutParams = p
        }
        buttonViewList.forEachIndexed { i, item ->
            item.view.setOnClickListener {
                val transaction = supportFragmentManager.beginTransaction()
                fragmentList.forEach {
                    transaction.hide(it)
                }
                buttonViewList.forEachIndexed { index, it ->
                    if (it.view == item.view) {
                        transaction.show(fragmentList[index])
                        it.tv.setTextColor(ContextCompat.getColor(this, mainColor))
                        it.iv.setImageResource(buttonRescouseList[index].selectResource)
                    } else {
                        it.tv.setTextColor(ContextCompat.getColor(this, R.color.text_grey))
                        if (buttonRescouseList[index].redTip) {
                            it.iv.setImageResource(buttonRescouseList[index].firstRescouse)
                        } else {
                            it.iv.setImageResource(buttonRescouseList[index].rescouse)
                        }
                    }
                }
                currentSelectBottom = i
                listener?.click(i, fragmentList[i])
                transaction.commit()
            }
        }
    }

    protected fun changeRedTip(index: Int, b: Boolean) {
        //因为获取到unReadCount的时候 是在列表页面，所以不用设置图标，只需要改状态就行
        buttonRescouseList[index].redTip = b
        if (index != currentSelectBottom) {
            if (buttonRescouseList[index].redTip) {
                buttonViewList[index].iv.setImageResource(buttonRescouseList[index].firstRescouse)
            } else {
                buttonViewList[index].iv.setImageResource(buttonRescouseList[index].rescouse)
            }
        }
    }

    protected fun selectPage(index: Int, mainColor: Int) {
        transaction = supportFragmentManager.beginTransaction()
        fragmentList.forEach {
            transaction.hide(it)
        }
        buttonViewList.forEachIndexed { i, item ->
            if (index == i) {
                transaction.show(fragmentList[index])
                item.tv.setTextColor(ContextCompat.getColor(this, mainColor))
                item.iv.setImageResource(buttonRescouseList[index].selectResource)
            } else {
                item.tv.setTextColor(ContextCompat.getColor(this, R.color.text_grey))
                if (buttonRescouseList[i].redTip) {
                    item.iv.setImageResource(buttonRescouseList[i].firstRescouse)
                } else {
                    item.iv.setImageResource(buttonRescouseList[i].rescouse)
                }
            }
        }

        currentSelectBottom = index
        listener?.click(index, fragmentList[index])
        transaction.commit()
    }

    fun getStatusBarHeightPx(): Int {
        var height = 0
        val resourceId = applicationContext.resources
            .getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            height =
                applicationContext.resources.getDimensionPixelSize(resourceId)
        }
        return height
    }
}